package com.bosicen.crm.web.controller.sysmanage;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bosicen.crm.dao.entity.OrderNode;
import com.bosicen.crm.dao.entity.User;
import com.bosicen.crm.service.sysmanage.OrderNodeServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.bosicen.crm.tools.Page;
import com.google.gson.Gson;

/***
 * 订单流程
 * @author tigerlei
 *
 */
@Controller
@RequestMapping("/basicinfo")
public class OrderNodeController {
	
	@Autowired
	public OrderNodeServiceImpl orderNodeService;
	
	
	/**
	 * 新增,修改信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addOrderNode/{type}")
	public String addorderProcess(HttpServletRequest request,@PathVariable("type") Integer type){
		String id = request.getParameter("id");
		OrderNode orderNode = null;
		if(type != 0 && StringUtils.isNotBlank(id)){
			orderNode = orderNodeService.findOrderNodeById(AppUtil.ObjectToInt(id));
		}
		request.setAttribute("orderNode", orderNode);
		return "sysmanage/addOrderNode";
	}
	
	
	/**
	 * 保存新增/修改、删除信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/cudOrderNode/{type}")
	public void saveOrderPorcess(OrderNode orderNode,HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		boolean flag=false;
		Gson gson = new Gson();
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			if(user != null){
				orderNode.setCompanyid(user.getCompanyid());
			}
			switch(type){
				case 0: flag = orderNodeService.delOrderNode(orderNode);break;
				case 1: flag = orderNodeService.saveOrUpdateOrderNode(orderNode);break;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/orderNodelist")
	public String findCompanylist(HttpServletRequest request) {
		Page page = new Page();
		page.setEveryPage(10);
		page.setCurrentPage(AppUtil.ObjectToInt(request.getParameter("pageno")));
		User user = (User) request.getSession().getAttribute("userinfo");
		page = orderNodeService.findOrderNodeByPage(user.getCompanyid(), page);
		request.setAttribute("page", page);
		return "sysmanage/OrderNode_list";
	}
	
}
